/*
 * Copyright 2009-2015 PrimeTek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.primefaces.component.panelmenu;

import java.io.IOException;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;

import org.primefaces.component.menu.AbstractMenu;
import org.primefaces.component.menu.BaseMenuRenderer;
import org.primefaces.model.menu.MenuElement;
import org.primefaces.model.menu.MenuItem;
import org.primefaces.model.menu.Submenu;

@FacesRenderer(componentFamily = "org.primefaces.component",
        rendererType = "org.primefaces.component.RoninMenuRenderer")
public class RoninMenuRenderer extends BaseMenuRenderer {

    @Override
    protected void encodeScript(FacesContext context, AbstractMenu abstractMenu) throws IOException {
//        PanelMenu menu = (PanelMenu) abstractMenu;
//        boolean stateful = menu.isStateful(); // PrimeFaces v5.2+
        boolean stateful = true;
        if (stateful) {
            ResponseWriter writer = context.getResponseWriter();
            writer.startElement("script", null);
            writer.writeAttribute("type", "text/javascript", null);
            writer.writeText("Ronin.restoreMenuState();", null);
            writer.endElement("script");
        }
    }

    @Override
    protected void encodeMarkup(FacesContext context, AbstractMenu abstractMenu) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        PanelMenu menu = (PanelMenu) abstractMenu;
        String style = menu.getStyle();
        String styleClass = menu.getStyleClass();

        writer.startElement("ul", menu);
//        writer.writeAttribute("id", menu.getClientId(context), "id");
        writer.writeAttribute("id", "layout-menu", "id"); // 使用硬编码的固定ID
        if (styleClass != null) {
            writer.writeAttribute("class", styleClass, "styleClass");
        }

        if (style != null) {
            writer.writeAttribute("style", style, "style");
        }

        if (menu.getElementsCount() > 0) {
            List<MenuElement> elements = menu.getElements();
            int menuLevel = 0;
            encodeElements(context, menu, elements, menuLevel);
        }

        writer.endElement("ul");
    }

    protected void encodeElements(FacesContext context, AbstractMenu menu, List<MenuElement> elements, int menuLevel) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        for (MenuElement element : elements) {
            if (element.isRendered()) {
                if (element instanceof MenuItem) {
                    MenuItem menuItem = (MenuItem) element;
                    writer.startElement("li", null);
                    writer.writeAttribute("id", menuItem.getClientId(), null);
                    writer.writeAttribute("role", "menuitem", null);
                    String containerStyleClass = menuItem.getContainerStyleClass();
                    if (containerStyleClass != null) {
                        writer.writeAttribute("class", containerStyleClass, null);
                    }
                    String containerStyle = menuItem.getContainerStyle();
                    if (containerStyle != null) {
                        writer.writeAttribute("style", containerStyle, null);
                    }
                    encodeMenuItem(context, menu, menuItem);
                    writer.endElement("li");
                } else if (element instanceof Submenu) {
                    encodeSubmenu(context, menu, (Submenu) element, menuLevel);
                }
            }
        }
    }

    @Override
    protected String getLinkStyleClass(MenuItem menuItem) {
        String styleClass = menuItem.getStyleClass();
        final String MENUITEM_LINK_CLASS = "menulink";
        return (styleClass == null) ? MENUITEM_LINK_CLASS : MENUITEM_LINK_CLASS + " " + styleClass;
    }

    @Override
    protected void encodeMenuItemContent(FacesContext context, AbstractMenu menu, MenuItem menuitem) throws IOException {
        ResponseWriter writer = context.getResponseWriter();

        String icon = menuitem.getIcon();
        if (icon != null) {
            writer.startElement("i", null);
            writer.writeAttribute("class", icon, null);
            writer.endElement("i");
        }

        Object value = menuitem.getValue();
        if (value != null) {
            writer.writeText(value, "value");
        }
    }

    protected void encodeSubmenu(FacesContext context, AbstractMenu menu, Submenu submenu, int menuLevel) throws IOException {
        ResponseWriter writer = context.getResponseWriter();

        writer.startElement("li", null);
        writer.writeAttribute("id", submenu.getClientId(), null);
        String styleClass = submenu.getStyleClass();
        if (styleClass != null) {
            writer.writeAttribute("class", styleClass, null);
        }
        String style = submenu.getStyle();
        if (style != null) {
            writer.writeAttribute("style", style, null);
        }
        writer.writeAttribute("role", "menuitem", null);

        //title
        writer.startElement("a", null);
        writer.writeAttribute("href", "#", null);
        final String SUBMENU_LINK_CLASS = "menulink";
        writer.writeAttribute("class", SUBMENU_LINK_CLASS, null);

        String icon = submenu.getIcon();
        if (icon != null) {
            writer.startElement("i", null);
            writer.writeAttribute("class", icon, null);
            writer.endElement("i");
        }

        String label = submenu.getLabel();
        if (label != null) {
            writer.writeText(label, "value");
        }

        if (menuLevel > 0) {
            encodeSubmenuIcon(context, submenu);
        }

        writer.endElement("a");

        //submenus and menuitems
        if (submenu.getElementsCount() > 0) {
            writer.startElement("ul", null);
            if (menuLevel > 0) {
                writer.writeAttribute("class", "menu-level-" + menuLevel, null);
            }
            writer.writeAttribute("role", "menu", null);

            encodeElements(context, menu, submenu.getElements(), ++menuLevel);

            writer.endElement("ul");
        }

        writer.endElement("li");
    }

    protected void encodeSubmenuIcon(FacesContext context, Submenu submenu) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        Object parent = submenu.getParent();
        if (parent != null) {
            writer.startElement("i", null);
            writer.writeAttribute("class", "fa fa-caret-down Fright", null);
            writer.endElement("i");
        }
    }
}
